﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Json.net
{
    public class JsonFun
    {
        public String GetJsonStrByDataTable(DataTable dt)
        {
            String rStr = "";
            JsonObject json = new JsonObject();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    json[dt.Columns[j].ColumnName] = new JsonProperty(dt.Rows[i][j].ToString().Trim());
                }
                if (i <(dt.Rows.Count-1))
                    rStr += json.ToString()+",";
                else
                    rStr += json.ToString() ;
            }
            return "["+rStr+"]";
        }
        /// <summary>
        /// 通过DATATABLE的到combox Json字符串
        /// </summary>
        /// <param name="dt">Datatable</param>
        /// <param name="V_Clm">value字段</param>
        /// <param name="T_Clm">Text字段</param>
        /// <returns></returns>
        public String GetComboxJsonStr(DataTable dt,String V_Clm,String T_Clm)
        {
            String rtStr = "";
            JsonFun jf = new JsonFun();
            JsonObject json = new JsonObject();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                json["id"] = new JsonProperty(dt.Rows[i][V_Clm].ToString().Trim());
                json["text"] = new JsonProperty(dt.Rows[i][T_Clm].ToString().Trim());
                if (i < (dt.Rows.Count - 1))
                    rtStr += json.ToString() + ",";
                else
                    rtStr += json.ToString();
            }
            return "[" + rtStr + "]";
        }
    }
}
